package com.yuhs.zixing.study.multithread.mashibing.t001base.t005.status;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * Function: 线程状态
 *
 * @Author yuhongsheng
 * @Date: 2023-12-20 21:25:55
 */
@Slf4j
public class ThreadStatus005Blocked {

    public static void main(String[] args) {

        testJoin();

    }


    @SneakyThrows
    private static void testJoin() {

        Thread threadA = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                log.atInfo().log("测试join 线程A {}", i);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread threadB = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                log.atInfo().log("测试join 线程B {}", i);
                if (i % 5 != 0) {
                    try {
                        threadA.join();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

            }
        });

        threadA.start();
        threadB.start();

        for (; ; ) {
            Thread.State state = threadB.getState();
            if (state.equals(Thread.State.WAITING)) {
                log.atInfo().log("线程状态thread1 {} ", state);
                break;
            }
        }
    }

}
